package com.ph.member.api.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ph.base.BaseController;
import com.ph.member.api.controller.qo.MemberCardQo;
import com.ph.member.api.service.MemberDataService;
import com.ph.shopping.common.core.constant.PageConstant;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.SmsCodeType;
import com.ph.shopping.common.util.container.ContainerUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.member.dto.IcCradindInfoDto;
import com.ph.shopping.facade.member.dto.MemberIcCardInfoPageDto;
import com.ph.shopping.facade.member.dto.UnbundMemberCardDto;
import com.ph.shopping.facade.member.entity.IcCardInfo;
import com.ph.shopping.facade.member.entity.Member;
import com.ph.shopping.facade.member.service.IMemberCardService;
import com.ph.shopping.facade.member.service.IMemberService;
import com.ph.shopping.facade.member.service.ISmsCodeSendService;
import com.ph.shopping.facade.member.vo.MemberCardInfoVo;
import com.ph.shopping.facade.permission.vo.SessionUserVo;

@Controller
@RequestMapping("api/membercard")
public class MemberCardController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(MemberCardController.class);
	
	/**
	 * 会员卡服务
	 */
	@Reference(version = "1.0.0", retries = 0, timeout = 30000)
	private IMemberCardService memberCardService;
	/**
	 * 短信服务
	 */
	@Reference(version = "1.0.0", retries = 0, timeout = 30000)
	private ISmsCodeSendService smsCodeSendService;
	/**
	 * 会员服务
	 */
	@Reference(version = "1.0.0", retries = 0, timeout = 30000)
	private IMemberService memberService;
	
	@Autowired
	private MemberDataService memberDataService;
	/**
	 * 
	* @Title: list  
	* @Description: 获取商户会员卡数据列表  
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/listPage")
	@ResponseBody
	public Result listPage(HttpServletRequest request, MemberCardQo qo) {
		log.info("加载会员卡列表参数 MemberCardQo = {} ", JSON.toJSONString(qo));
		Result result = getResult(ResultEnum.MEMBER_NO_DATA);
		try {
			MemberIcCardInfoPageDto dto = new MemberIcCardInfoPageDto();
			BeanUtils.copyProperties(qo, dto);
			SessionUserVo user = getLoginUser();
			if(user == null || user.getId() == null){
				log.debug("未获取到当前登录人信息");
			}else{
				dto.setMerchantId(user.getId());
				if (dto.getPageNum() == null) {
					dto.setPageNum(PageConstant.pageNum);
				}
				if (dto.getPageSize() == null) {
					dto.setPageSize(PageConstant.pageSize);
				}
				result = memberCardService.getMemberBindCardByPage(dto);
			}
		} catch (Exception e) {
			log.error("加载会员卡数据列表错误", e);
		}
		return result;
	}
	/**
	 * 
	* @Title: listMemberPage  
	* @Description: 加载商户会员卡页面 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value="/merchantMemberCardList",method=RequestMethod.GET)
	public String list(){
		
		return "/membercard/list";
	}
	/**
	 * 
	* @Title: addmembercard  
	* @Description:   会员卡发卡页面
	* @param @param request
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/bindMemberCardView")
	public String addmembercard(HttpServletRequest request){
		
		return "membercard/bindMemberCardView";
	}
	/**
	 * 
	* @Title: addmembercard  
	* @Description: 加载挂失会员卡页面  
	* @param @param request
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/cancelMemberCardView")
	public String cancelMemberCardView(HttpServletRequest request){
		
		return "membercard/cancelMemberCardView";
	}
	/**
	 * 
	* @Title: addMemberCard  
	* @Description: 加载添加会员卡页面
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value="/addMemberCard",method=RequestMethod.GET)
	public String addMemberCard(){
		
		return "/membercard/addMemberCard";
	}
	/**
	 * 
	* @Title: list  
	* @Description:加载所有会员卡数据  
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value="/allMemberCardList",method=RequestMethod.GET)
	public String allist(){
		
		return "/membercard/allMemberCardList";
	}
	/**
	 * 
	* @Title: memberCardList  
	* @Description: 加载会员卡列表
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value="/memberCardListView",method=RequestMethod.GET)
	public String memberCardListView(){
		
		return "/membercard/memberCardListView";
	}
	/**
	 * 
	* @Title: importMemberCardView  
	* @Description: 导入会员卡  
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value="/importMemberCardView",method=RequestMethod.GET)
	public String importMemberCardView(){
		
		return "/membercard/importMemberCardView";
	}
	/**
	 * 
	* @Title: memberCardList  
	* @Description: 加载会员卡列表 
	* @param @param request
	* @param @param qo
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	@RequestMapping("/memberCardList")
	@ResponseBody
	public Result memberCardList(HttpServletRequest request, MemberCardQo qo) {
		log.info("加载会员卡列表参数 MemberCardQo = {} ", JSON.toJSONString(qo));
		Result result = getResult(ResultEnum.MEMBER_NO_DATA);
		try {
			MemberIcCardInfoPageDto dto = new MemberIcCardInfoPageDto();
			BeanUtils.copyProperties(qo, dto);
			if (dto.getPageNum() == null) {
				dto.setPageNum(PageConstant.pageNum);
			}
			if (dto.getPageSize() == null) {
				dto.setPageSize(PageConstant.pageSize);
			}
			result = memberCardService.getMemberCardByPage(dto);
			log.info("加在会员卡列表返回参数 Result = {}",JSON.toJSONString(result));
		} catch (Exception e) {
			log.error("加载会员卡数据列表错误", e);
		}
		return result;
	}
	/**
	 * 
	* @Title: alllistPage  
	* @Description: 平台查询所有绑定数据  
	* @param @param request
	* @param @param qo
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	@RequestMapping("/alllistPage")
	@ResponseBody
	public Result alllistPage(HttpServletRequest request, MemberCardQo qo) {
		log.info("加载会员卡列表参数 MemberCardQo = {} ", JSON.toJSONString(qo));
		Result result = getResult(ResultEnum.MEMBER_NO_DATA);
		try {
			MemberIcCardInfoPageDto dto = new MemberIcCardInfoPageDto();
			BeanUtils.copyProperties(qo, dto);
			if (dto.getPageNum() == null) {
				dto.setPageNum(PageConstant.pageNum);
			}
			if (dto.getPageSize() == null) {
				dto.setPageSize(PageConstant.pageSize);
			}
			result = memberCardService.getMemberBindCardByPageAndAll(dto);
		} catch (Exception e) {
			log.error("加载会员卡数据列表错误", e);
		}
		return result;
	}
	/**
	 * 
	* @Title: frozenMemberCard  
	* @Description: 冻结会员卡  
	* @param @param request
	* @param @param icCardId
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/frozenMemberCard")
	@ResponseBody
	public Map<String, Object> frozenMemberCard(HttpServletRequest request, Long icCardId) {
		log.info("冻结会员卡会员卡参数icCardId: " + icCardId);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			SessionUserVo user = getLoginUser();
			if(user == null || user.getId() == null){
				log.debug("未获取到当前登录人信息");
			}else{
				UnbundMemberCardDto dto = new UnbundMemberCardDto();
				dto.setIcCardId(icCardId);;
				dto.setOperatorUserId(user.getId());
				Result result = memberCardService.frozenMemberCard(dto);
				log.info("冻结会员卡返回数据 Result = {} ", JSON.toJSONString(result));
				if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					map.put("code", result.getCode());
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("冻结会员卡错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: relieveMemberCard  
	* @Description: 解冻会员卡  
	* @param @param request
	* @param @param icCardId
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/relieveMemberCard")
	@ResponseBody
	public Map<String, Object> relieveMemberCard(HttpServletRequest request, Long icCardId) {
		log.info("解冻会员卡参数icCardId: " + icCardId);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			SessionUserVo user = getLoginUser();
			if(user == null || user.getId() == null){
				log.debug("未获取到当前登录人信息");
			}else{
				UnbundMemberCardDto dto = new UnbundMemberCardDto();
				dto.setIcCardId(icCardId);;
				dto.setOperatorUserId(user.getId());
				Result result = memberCardService.relieveMemberCard(dto);
				log.info("解冻会员卡返回数据 Result = {} ", JSON.toJSONString(result));
				if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					map.put("code", result.getCode());
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("解冻会员卡错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: cancelMemberCard  
	* @Description: 挂失会员卡
	* @param @param request
	* @param @param dto
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/cancelMemberCard")
	@ResponseBody
	public Map<String, Object> cancelMemberCard(HttpServletRequest request, String phone) {
		log.info("挂失会员卡参数phone: " + phone);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			SessionUserVo user = getLoginUser();
			if(user == null || user.getId() == null){
				log.debug("未获取到当前登录人信息");
			}else{
				UnbundMemberCardDto dto = new UnbundMemberCardDto();
				dto.setPhone(phone);
				dto.setOperatorUserId(user.getId());
				Result result = memberCardService.cancelMemberCard(dto);
				log.info("挂失会员卡返回数据 Result = {} ", JSON.toJSONString(result));
				if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					map.put("code", result.getCode());
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("挂失会员卡错误", e);
		}
		return map;
	}
	
	/**
	 * 
	* @Title: getMemberCardByPhone  
	* @Description: 根据手机号获取会员卡信息  
	* @param @param request
	* @param @param phone
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/getMemberCardByPhone")
	@ResponseBody
	public Map<String, Object> getMemberCardByPhone(HttpServletRequest request, String phone) {
		log.info("根据手机卡号获取会员卡数据据参数phone: " + phone);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			Result result = memberCardService.getMemberIcCardInfoByPhone(phone);
			log.info("根据手机卡号获取会员卡数据返回数据 Result = {} ", JSON.toJSONString(result));
			if (result != null) {
				if (ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					Object obj = result.getData();
					if (obj instanceof MemberCardInfoVo) {
						MemberCardInfoVo cardIinfo = (MemberCardInfoVo) obj;
						map.put("code", result.getCode());
						map.put("cardIinfo", cardIinfo);
					}
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("根据手机卡号获取会员卡数据错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: verifySmsCode  
	* @Description: 根据手机号获取短信验证码  
	* @param @param request
	* @param @param phone
	* @param @param smsCode
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/verifySmsCode")
	@ResponseBody
	public Map<String, Object> verifySmsCode(HttpServletRequest request, String phone, String smsCode) {
		log.info("根据手机号获取短信验证码 phone: " + phone + " smsCode : " + smsCode);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			Result result = smsCodeSendService.verifySmsCode(phone, SmsCodeType.SEND_SMSCODE.getCodeType(), smsCode);
			log.info("根据手机号获取短信验证码返回数据 Result = {} ", JSON.toJSONString(result));
			if (result != null) {
				String code = result.getCode();
				if (ResultEnum.SUCCESS.getCode().equals(code)) {
					map.put("code", result.getCode());
				} else if (ResultEnum.SMS_CODE_INVALID.getCode().equals(code)) {
					map.put("msg", ResultEnum.SMS_CODE_INVALID.getMsg());
				} else {
					map.put("msg", "验证码错误");
				}
			}
		} catch (Exception e) {
			log.error("根据手机号获取短信验证码错误", e);
		}
		return map;
	}
	/***
	 * 
	* @Title: sendSmsCode  
	* @Description: 发送短信验证码  
	* @param @param request
	* @param @param phone
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/sendSmsCode")
	@ResponseBody
	public Map<String, Object> sendSmsCode(HttpServletRequest request, String phone) {
		log.info("发送短信验证码 phone: " + phone);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			Result result = smsCodeSendService.sendSmsCodeByNoMsg(phone, SmsCodeType.SEND_SMSCODE.getCodeType());
			log.info("发送短信验证码 返回数据 Result = {} ", JSON.toJSONString(result));
			if (result != null) {
				if (ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					map.put("code", result.getCode());
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("发送短信验证码 错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: bindMemberCard  
	* @Description: 会员卡发卡  
	* @param @param request
	* @param @param phone
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/bindMemberCard")
	@ResponseBody
	public Map<String, Object> bindMemberCard(HttpServletRequest request, IcCradindInfoDto dto) {
		log.info("会员发卡参数 IcCradindInfoDto = {} ", JSON.toJSONString(dto));
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			SessionUserVo user = getLoginUser();
			if(user == null || user.getId() == null){
				log.debug("未获取到当前登录人信息");
			}else{
				dto.setMerchantId(user.getId());
				Result result = memberCardService.bindMemberCard(dto);
				log.info("会员发卡 返回数据 Result = {} ", JSON.toJSONString(result));
				if (result != null) {
					if (ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
						map.put("code", result.getCode());
					} else {
						map.put("msg", result.getMessage());
					}
				}
			}
		} catch (Exception e) {
			log.error("会员发卡错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: getMemberInfoByPhone  
	* @Description: 根据手机号获取会员信息 
	* @param @param request
	* @param @param phone
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/getMemberInfoByPhone")
	@ResponseBody
	public Map<String, Object> getMemberInfoByPhone(HttpServletRequest request, String phone) {
		log.info("根据手机号获取会员参数phone: " + phone);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			Result result = memberService.getMemberInfoByMobile(phone);
			log.info("根据手机号获取会员返回数据 Result = {} ", JSON.toJSONString(result));
			if (result != null) {
				if (ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					Object obj = result.getData();
					if (obj instanceof Member) {
						Member member = (Member) obj;
						map.put("code", result.getCode());
						map.put("member", member);
					}
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("根据手机卡号获取会员卡数据错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: addMemberCradSubmit  
	* @Description: 添加会员卡操作
	* @param @param request
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/addMemberCradSubmit")
	@ResponseBody
	public Map<String, Object> addMemberCradSubmit(HttpServletRequest request, String innerCode, String outerCode) {
		log.info("添加会员卡参数innerCode: " + innerCode + " >> outerCode: " + outerCode);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			IcCardInfo icInfo = new IcCardInfo();
			icInfo.setInnerCode(innerCode);
			icInfo.setOuterCode(outerCode);
			Result result = memberCardService.addMemberCard(icInfo);
			log.info("添加会员卡返回数据 Result = {} ", JSON.toJSONString(result));
			if (result != null) {
				if (ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					map.put("code", result.getCode());
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("添加会员卡数据错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: importMemberCard  
	* @Description: 导入会员操作
	* @param @param request
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/importMemberCard")
	@ResponseBody
	public Map<String, Object> importMemberCard(HttpServletRequest request) {
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			String[] names = { "innerCode", "outerCode" };
			List<IcCardInfo> list = memberDataService.getExcelImportInfos(IcCardInfo.class, names, request);
			log.info("导入会员卡数据 = {} ", JSON.toJSONString(list));
			Result result = memberCardService.batchAddMemberCard(list);
			log.info("导入会员卡返回数据 = {} ", JSON.toJSONString(result));
			if (result != null) {
				if (ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					map.put("code", result.getCode());
					Object obj = result.getData();
					int successNum = 0;
					int errorNum = 0;
					if(obj instanceof List){
						List<?> errors = (List<?>)obj;
						errorNum = errors.size();
						successNum = list.size() - errorNum;
					}
					map.put("successNum", successNum);
					map.put("errorNum", errorNum);
				} else {
					map.put("msg", result.getMessage());
				}
			}
		} catch (Exception e) {
			log.error("导入会员错误", e);
		}
		return map;
	}
}
